package com.atguigu.ssyx.activity.service.impl;

import com.atguigu.ssyx.activity.mapper.CouponInfoMapper;
import com.atguigu.ssyx.activity.mapper.CouponRangeMapper;
import com.atguigu.ssyx.activity.service.CouponInfoService;
import com.atguigu.ssyx.client.product.ProductFeignClient;
import com.atguigu.ssyx.model.activity.CouponInfo;
import com.atguigu.ssyx.model.activity.CouponRange;
import com.atguigu.ssyx.model.product.Category;
import com.atguigu.ssyx.model.product.SkuInfo;
import com.atguigu.ssyx.vo.activity.CouponRuleVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 优惠券信息 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2025-11-14
 */
@Service
public class CouponInfoServiceImpl extends ServiceImpl<CouponInfoMapper, CouponInfo> implements CouponInfoService {

    @Resource
    private CouponRangeMapper couponRangeMapper;

    @Resource
    private ProductFeignClient productFeignClient;

    /**
     * 优惠券列表
     */
    @Override
    public IPage<CouponInfo> selectedPage (Long page, Long limit) {
        Page<CouponInfo> pageParam = new Page<>(page, limit);
        IPage<CouponInfo> pageModel = baseMapper.selectPage(pageParam, null);
        // 获取数据
        pageModel.getRecords().forEach(item -> {
            // 获取优惠券类型
           item.setCouponTypeString(item.getCouponType().getComment());
           if (item.getRangeType() != null) {
               // 获取优惠券使用范围
               item.setRangeTypeString(item.getRangeType().getComment());
           }
        });
        return pageModel;
    }

    /**
     * 根据id查询优惠券信息
     */
    @Override
    public CouponInfo getCouponInfoById (Long id) {
        CouponInfo couponInfo = baseMapper.selectById(id);
        if (couponInfo != null) {
            couponInfo.setCouponTypeString(couponInfo.getCouponType().getComment());
            if (couponInfo.getRangeType() != null) {
                couponInfo.setRangeTypeString(couponInfo.getRangeType().getComment());
            }
        }
        return couponInfo;
    }

    /**
     * 根据id查询优惠券规则数据
     */
    @Override
    public Map<String, Object> findCouponRuleList (Long id) {
        // 获取优惠券信息
        CouponInfo couponInfo = baseMapper.selectById(id);
        // 获取对应的rangeId
        LambdaQueryWrapper<CouponRange> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CouponRange::getCouponId, id);
        List<CouponRange> couponRangeList = couponRangeMapper.selectList(wrapper);
        // 封装数据
        List<Long> rangeIdList = couponRangeList.stream().map(CouponRange::getRangeId)
                .collect(Collectors.toList());
        // 判断优惠券类型，如果优惠券类型为sku，则获取skuId，如果优惠券类型为分类，则获取分类Id
        Map<String, Object> result = new HashMap<>();
        // 封装不同的rangeId的数据
        if (!CollectionUtils.isEmpty(rangeIdList)) {
            switch (couponInfo.getRangeType()) {
                case SKU:
                    // 如果优惠券类型为sku，则获取skuId，远程调用根据多个skuId获得sku信息
                    List<SkuInfo> skuInfoList = productFeignClient.findSkuInfoList(rangeIdList);
                    result.put("skuInfoList", skuInfoList);
                    break;
                case CATEGORY:
                    // 如果优惠券类型为分类，则获取分类Id, 远程调用根据多个分类Id获得分类信息
                    List<Category> categoryList = productFeignClient.findCategoryList(rangeIdList);
                    result.put("categoryList", categoryList);
                    break;
            }
        }
        return result;
    }

    /**
     * 添加优惠券规则
     */
    @Override
    public void saveCouponRule (CouponRuleVo couponRuleVo) {
        // 删除之前规则数据
        LambdaQueryWrapper<CouponRange> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CouponRange::getCouponId, couponRuleVo.getCouponId());
        couponRangeMapper.delete(wrapper);

        // 更新优惠券基本信息
        CouponInfo couponInfo = baseMapper.selectById(couponRuleVo.getCouponId());
        couponInfo.setRangeType(couponRuleVo.getRangeType());
        couponInfo.setConditionAmount(couponRuleVo.getConditionAmount());
        couponInfo.setAmount(couponRuleVo.getAmount());
        couponInfo.setRangeDesc(couponRuleVo.getRangeDesc());
        baseMapper.updateById(couponInfo);

        // 添加新的规则数据
        // SKU
        List<CouponRange> couponRangeList = couponRuleVo.getCouponRangeList();
        if (!CollectionUtils.isEmpty(couponRangeList)) {
            couponRangeList.forEach(couponRange -> {
                couponRange.setCouponId(couponRuleVo.getCouponId());
                couponRangeMapper.insert(couponRange);
            });
        }
    }
}
